package com.canaan.business.schedule;

import com.canaan.business.common.enums.agv.AgvEnum;
import com.canaan.business.common.enums.storehouse.StorehouseStockStatusEnum;
import com.canaan.business.common.helper.EmptyStockHelper;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.domain.vo.WmsStorehouseStockVo;
import com.canaan.business.service.agv.IAgvTaskDispatchService;
import com.canaan.business.service.basic.IWmsStorehouseLocationService;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.core.redis.RedisCache;
import com.canaan.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Component("AutoEmptyShelfEnterStockShedule")
public class AutoEmptyShelfEnterStockShedule {
    @Resource
    private EmptyStockHelper emptyStockHelper;
    @Resource
    private IWmsStorehouseStockService storehouseStockService;
    @Resource
    private IWmsStorehouseLocationService locationService;
    @Resource
    private IAgvTaskDispatchService dispatchService;
    @Resource
    private RedisCache redisCache;

    @ApiOperation(value = "空料架入库", notes = "空料架入库")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void emptyShelfEnterStock(){
        String key = "IN_LOCK";
        //TODO 临时方案锁，后期处理
        String lock = redisCache.getCacheObject(key);
        if (StringUtils.equals(lock, "true")) {
            throw new RuntimeException("有出库任务正在进行中，请稍后再试");
        }
        redisCache.setCacheObject(key, "true");
        try {
            emptyShelfEnterStockLock();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            //TODO 临时方案
            redisCache.setCacheObject(key, "false");
        }
    }

    /**
     * 空料架组入库锁逻辑
     */
    private void emptyShelfEnterStockLock(){
        WmsStorehouseStockVo outDockingEmptyShelfStockVo=storehouseStockService.selectOutDockingEmptyShelfStorehouseStock();
        if(outDockingEmptyShelfStockVo!=null){
            WmsStorehouseLocation outDockingLocation=locationService.selectWmsStorehouseLocationByNo(outDockingEmptyShelfStockVo.getStorehouseLocationNo());
            if(outDockingLocation==null){
                return;
            }
            WmsStorehouseStock sourceStock=storehouseStockService.selectWmsStorehouseStockByLocationNo(outDockingLocation.getNo());
            if(sourceStock==null){
                return;
            }

            WmsStorehouseLocation destLocation= emptyStockHelper.analyseInStock("","",false,true);
            //创建AGV任务
            dispatchService.createTaskDispatch("",outDockingLocation,
                    destLocation, AgvEnum.TASK_TYPE.IN,sourceStock.getContainerNo(),sourceStock.getTrayNo());
            sourceStock.setStatus(StorehouseStockStatusEnum.LOCK.getCode());
            storehouseStockService.updateWmsStorehouseStock(sourceStock);
        }
    }
}
